﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QldtSdh.GUI.DaoTao
{
    using BLL;
    using DAL;
    using DAL.Models;
    using GUI.Utils;
    using GUI.Common;
    using Utils;

    public partial class FormInThoiKhoaBieuGiangVien_Batch : BaseDialog
    {
        private List<GiangVien> _GiangVienCollection;
        private DonVi _DonVi;
        private HocKy _HocKy;

        public FormInThoiKhoaBieuGiangVien_Batch(DonVi donVi, List<GiangVien> lstGiangVien, HocKy hocKy)
        {
            InitializeComponent();

            _HocKy = hocKy;
            _DonVi = donVi;
            _GiangVienCollection = lstGiangVien;
            cbxLoaiTep.SelectedIndex = 0;

            this.Text = "In thời khoá biểu Giảng viên";
            lblHeader.Text = string.Format("In thời khoá biểu {0} của giảng viên trong {1}", _HocKy.TenHocKyDayDu, _DonVi.TenDonVi);
        }

        private void UpdateGuiBySelectedTypeOfExport()
        {
            panel1.Enabled = radInRaTep.Checked;
        }

        private void radInRaGiay_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGuiBySelectedTypeOfExport();
        }

        private void btnChonThuMuc_Click(object sender, EventArgs e)
        {
            var dlg = new FolderBrowserDialog();
            dlg.Description = "Chọn đường dẫn chứa tệp...";
            dlg.ShowNewFolderButton = true;
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                txtDuongDanThuMuc.Text = dlg.SelectedPath;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            var docType = cbxLoaiTep.SelectedIndex == 0 ? QldtSdh.Utils.PrintUtil.DocType.Word : QldtSdh.Utils.PrintUtil.DocType.Excel;
            var docExt = docType == QldtSdh.Utils.PrintUtil.DocType.Excel ? ".XLS" : ".DOC";
            var stFolder = txtDuongDanThuMuc.Text;
            if (System.IO.Directory.Exists(stFolder) == false && radInRaGiay.Checked == false)
            {
                DialogUtils.ShowErrorWithDefaultTitle("Thư mục \"{0}\" chưa tồn tại!", stFolder);
                return;
            }

            var bw = new BackgroundWorker();
            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;
            bw.DoWork += (oSender, oE) =>
            {
                var frm = new FormInThoiKhoaBieuGiangVien();
                var pHocKyNamHocString = _HocKy.TenHocKyDayDu;
                var pDonViPhuTrach = _DonVi.TenDonVi;
                var pChucDanhKy = "P. TRƯỞNG PHÒNG ĐTSĐH";
                var pHoTenChucDanhKy =
                    //BizConfig.GetConfig()["phophong"].ToUpper();
                    BizAppConfig.GetPhoTruongPhong();

                for (int i = 0; i < _GiangVienCollection.Count; i++)
                {
                    if (bw.CancellationPending)
                        return;
                    var giangVien = _GiangVienCollection[i];
                    bw.ReportProgress(0, string.Format("{0}/{1} - Đang in {2}", i + 1, _GiangVienCollection.Count, giangVien.GiangVienDescTKB));

                    var pGiangVien = giangVien.GiangVienDescTKB;
                    var lstThoiKhoaBieu = (BizThoiKhoaBieu.GetThoiKhoaBieuCacLHP(giangVien, _HocKy));
                    frm.LoadData(lstThoiKhoaBieu, pGiangVien, pDonViPhuTrach, pHocKyNamHocString, pChucDanhKy, pHoTenChucDanhKy);
                    if (radInRaGiay.Checked)
                        QldtSdh.Utils.PrintUtil.DoPrint(frm.LocalReport, false);
                    else
                        QldtSdh.Utils.PrintUtil.DoExport(frm.LocalReport, docType, System.IO.Path.Combine(stFolder,
                            string.Format("{0}{1}", giangVien.MaGiangVien, docExt)), false);
                }
            };
            bw.ProgressChanged += (oSender, oE) =>
            {
                lblStatus.Text = oE.UserState.ToString();
            };
            bw.RunWorkerCompleted += (oSender, oE) =>
            {
                btnDongY.Enabled = true;
                pnlInput.Enabled = true;
                pnlProgress.Visible = false;
                btnBoQua.Tag = null;
                if (this.Visible && radInRaGiay.Checked == false)
                    DialogUtils.ShowMessage("Đã in xong");
            };
            btnDongY.Enabled = false;
            pnlInput.Enabled = false;
            pnlProgress.Visible = true;
            btnBoQua.Tag = bw;
            bw.RunWorkerAsync();
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            if (btnBoQua.Tag != null)
                (btnBoQua.Tag as BackgroundWorker).CancelAsync();
            else
                this.Close();
        }

        private void FormInBangChamDiemQTHT_Batch_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = btnBoQua.Tag != null;
        }
    }
}
